package com.ruoyi.flow.auth.security.sso;

import com.ruoyi.flow.auth.security.sso.SsoUsernamePasswordProvider;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wtian
 * @version 1.0
 * @since 2018/9/12 15:05
 */
@Component
public class SsoRequestContext {
    private static final ThreadLocal<SsoUsernamePasswordProvider> provider = new ThreadLocal<>();
    private List<SsoUsernamePasswordProvider> providers;

    public List<SsoUsernamePasswordProvider> getProviders() {
        return providers;
    }

    public void setProviders(List<SsoUsernamePasswordProvider> providers) {
        this.providers = providers;
    }

    public void init(HttpServletRequest request) {
        for (SsoUsernamePasswordProvider usernamePasswordProvider : providers) {
            if (usernamePasswordProvider.matcher().matches(request)) {
                provider.set(usernamePasswordProvider);
                break;
            }
        }
    }

    public SsoUsernamePasswordProvider get() {
        return provider.get();
    }

}
